/**************************************************************************************************

	NSurvey - The web survey and form engine
	Copyright (c) 2004, Thomas Zumbrunn. (http://www.nsurvey.org)

	Dotnetnuke PA module 
	Copyright (c) 2004, Oliver Hine (oliver.hine@gmail.com)

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


************************************************************************************************/
using DotNetNuke;
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Votations.NSurvey.DataAccess;
using Votations.NSurvey.Data;
using Votations.NSurvey.WebAdmin.UserControls;
using Votations.NSurvey.WebControls;

namespace Votations.NSurvey.WebAdmin
{
	public class ControlCode : PageBase
	{
		protected SurveyListControl SurveyList;
		protected System.Web.UI.WebControls.Label MessageLabel;
		protected HeaderControl Header;
		protected System.Web.UI.WebControls.TextBox tbNetSource;
		protected System.Web.UI.WebControls.HyperLink CodeHyperLink;
		protected System.Web.UI.WebControls.Literal ControlCodeTitle;
		protected System.Web.UI.WebControls.Literal PageDirectiveInfo;
		protected System.Web.UI.WebControls.Literal TagPrefixInfo;
		protected System.Web.UI.WebControls.Literal QuickLinkInfo;
		protected System.Web.UI.WebControls.Literal SurveyCodePreviewTitle;
		protected SurveyBox SurveyPreview;

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
		}
		#endregion

		private void Page_Load(object sender, System.EventArgs e)
		{
			try
			{
				SetupSecurity();
				LocalizePage();

				if (!Page.IsPostBack)
				{
					SurveyList.SurveyId = SurveyId;
					SurveyList.BindDropDown();
					Header.SurveyId = SurveyId;
					SurveyPreview.SurveyId = SurveyId;
					CodeHyperLink.NavigateUrl = CurrentFolder("survey.aspx?surveyid=" + SurveyId);
					
					
					if (Request.ServerVariables["SERVER_PORT"] != null && Request.ServerVariables["SERVER_PORT"] != "80" )
					{

						CodeHyperLink.NavigateUrl = string.Format("http://{0}:{1}/" + CurrentFolder("survey.aspx") + "?surveyid={2}", 
							HttpContext.Current.Request.Url.Host, Request.ServerVariables["SERVER_PORT"], SurveyId);

					}
					else
					{

						CodeHyperLink.NavigateUrl = string.Format("http://{0}/" + CurrentFolder("survey.aspx") + "?surveyid={1}", 
							HttpContext.Current.Request.Url.Host, SurveyId);

					}
					CodeHyperLink.Text = CodeHyperLink.NavigateUrl;
				}
			}
			catch( Exception exc)
			{
				DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException(this,exc);
			}
		}

		private void SetupSecurity()
		{
			CheckRight(NSurveyRights.AccessASPNetCode, true);
		}

		private void LocalizePage()
		{
			ControlCodeTitle.Text = GetPageResource("ControlCodeTitle");
			PageDirectiveInfo.Text = GetPageResource("PageDirectiveInfo");
			TagPrefixInfo.Text = Server.HtmlEncode(GetPageResource("TagPrefixInfo"));
			QuickLinkInfo.Text = GetPageResource("QuickLinkInfo");
			SurveyCodePreviewTitle.Text = GetPageResource("SurveyCodePreviewTitle");
		}

	}
}
